package cn.com.wdjedu.androidapp.wjeduandroid.ui.baseui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.ViewConfiguration;

import java.lang.reflect.Field;

import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.global.GlobalContext;

/**
 * User: Jiang Qi
 * Date: 12-7-31
 * 基本的类型Activity 主要是为了解决主题切换
 */
public abstract class AbstractAppActivity extends FragmentActivity {
    public static final String EXTRA_LOGIN_INFO = "loginInfo";

    protected int theme = 0;


    @Override
    protected void onResume() {
        super.onResume();

//        if (theme != SettingUtility.getAppTheme()) {
//            reload();
//        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (GlobalContext.getInstance().getCurrentRunningActivity() == this) {
            GlobalContext.getInstance().setCurrentRunningActivity(null);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("theme", theme);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
//        if (savedInstanceState == null) {
//            theme = SettingUtility.getAppTheme();
//        } else {
//            theme = savedInstanceState.getInt("theme");
//        }
//        setTheme(theme);
        super.onCreate(savedInstanceState);
        forceShowActionBarOverflowMenu();
        GlobalContext.getInstance().setActivity(this);
    }


    private void forceShowActionBarOverflowMenu() {
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception ignored) {

        }
    }

    private void reload() {
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();

        overridePendingTransition(0, 0);
        startActivity(intent);
    }


}
